package com.practice.designMode.Iterator;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class TestObject implements Iterable<String>{
    private String name;
    private int age;

    public TestObject(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public Iterator<String> iterator() {
        return new TestObjectIterator();
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "TestObject{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    class TestObjectIterator implements Iterator<String> {

        int count = 2;


        @Override
        public boolean hasNext() {
            return count > 0;
        }

        @Override
        public String next() {
            if(count == 2) {
                count--;
                return name;
            } else if (count == 1) {
                count--;
                return String.valueOf(age);
            }
            throw new NoSuchElementException();
        }
    }
}
